package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.swing.text.html.FormSubmitEvent;
import java.util.HashMap;
import java.util.Random;

@Component
public class CommonRpc {
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";
    static final String accessKeyId = "LTAISniRZCP24izc";
    static final String accessKeySecret = "biItjRacDnnyq29yixu1z5Gf6ICJkb";

    /**
     * 阿里云发送验证短信码接口
     *
     */
    public String  sendBatchSms(String phone){
        String YZCode = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            YZCode += random.nextInt(10);
        }

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "东陵网络科技");
        request.putQueryParameter("TemplateCode", "SMS_158895140");
        HashMap map = new HashMap();
        map.put("code", YZCode);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return  YZCode;
    }

    public static void main(String args[]){
        CommonRpc commonRpc = new CommonRpc();
        commonRpc.sendBatchSms("13949667279");
    }

}
